const api = require('../../config/home.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: "",
    isShowProp: false,
    showProp: false,
    swiperCurrent: 0,
    indicatorDots: true,
    autoplay: false,
    interval: 2000,
    duration: 500,
    durations: 8000,
    interval: 30,
    circular: true,
    imgUrls: [
    ],
    informationCategorys: [],
    informationCategoryGYS: [],
    imgUrls1: 'https://p3.pstatp.com/large/43700001e49d85d3ab52',
    value: '',
    homeList: [],
    supplierList: [],
    adList: [],
    passSearchClassify: 1,
    active: 1,

  },
  swiperChange(e) {
    let { current, source } = e.detail
    if (source === 'autoplay' || source === 'touch') {
      this.setData({
        swiperCurrent: current
      })
    }
  },
  onTypeClick(e) {
    this.setData({
      active: Number(e.currentTarget.dataset.type || 1)
    })
  },
  swipclick(e) {
    const { advertisementtype, id } = e.target.dataset
    wx.navigateTo({
      url: `/pages/everyLunbo/index?advertisementtype=${advertisementtype}&id=${id}`
    });
  },
  goSearch(e) {
    console.log("获取焦点",e)
    this.setData({
      focus:false
    })
    if (this.data.active == 2) {
      wx.navigateTo({
        url: `/pages/price/index`
      });
    } else {
      wx.navigateTo({
        url: `/pages/searchDetail/index?classify=${this.data.passSearchClassify}`
      });
    }

  },
  onClick() {
    wx.navigateTo({
      url: `/pages/registerPrese/index`
    });
  },
  gomore() {
    wx.navigateTo({
      url: '/pages/more/index'
    })
  },
  lunboList() {
    wx.request({
      url: api.lunboList, //仅为示例，并非真实接口地址。
      data: {
        current: 1,
        pageSize: 4
      },
      method: 'GET',
      success: (res) => {
        if (res.data.code === 100) {
          let obj = res.data.object.records
          this.setData({
            imgUrls: obj
          })
        }

      }
    });
  },
  getDictDataList() {
    wx.request({
      url: api.dictDataListUrl,
      data: {
        type: 'information_category'
      },
      method: 'GET',
      success: (res) => {
        if (res.data.code === 200) {
          let data = res.data.data
          if (data && data.length) {
            let gys = data.filter(item => item.dictSort == 2)
            let informationCategorys = data.filter(item => item.dictSort == 1)
            this.setData({
              informationCategorys: informationCategorys || [],
              informationCategoryGYS: gys || []
            })
          } else {
            this.setData({
              informationCategorys: [],
              informationCategoryGYS: []
            })
          }

        }
      }
    });
  },
  onChangeTab(e) {
    const { name } = e.detail
    if (name == 0) {
      this.setData({
        passSearchClassify: 0
      })
    } else {
      this.setData({
        passSearchClassify: 1
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取分享者sender的传递参数id
    let _this = this
    getApp().userInfoReadyCallback = res => {
      _this.valided()
    }
    let sender = options.sender;
    if (sender != null) {
      //说明是通过分享页面进入
      wx.setStorageSync('sender', sender)
      wx.showModal({
        title: '提示',
        content: '去登录即可成为体验会员！',
        success(res) {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/my/index?sender=' + sender,
            })
          } else if (res.cancel) {
          }
        }
      })
    }
    this.lunboList()
    this.getDictDataList()
  },
  valided() {
		this.setData({
			showProp: false
		})
    let hasUser = getApp().hasUser()
		if (!hasUser) {
			this.setData({
        showProp: true,
        onConfirmId:0
      })
      return false
		} else  {
      let hasPhone = getApp().hasPhone()
      console.log("hasPhone status",hasPhone) 
      if(!hasPhone){
        let user = wx.getStorageSync('wxUser')
        let wxName = user.wxName
        let wxUrl = user.wxUrl
        console.log("hasPhone user ",user)
        this.setData({
          showProp: true,
          onConfirmId:1,
          userInfo:{
            avatarUrl:wxUrl,
            nickName:wxName
          }
        })
        return false
      }
      console.log("hasPhone",this.data.onConfirmId) 
    }
    return true
	},
  // valided() {
  //   this.setData({
	// 		showProp: false
	// 	})
  //   // debugger
  //   // if (this.data.isShowProp) {
  //   //   return true
  //   // }
  //   this.setData({
  //     showProp: false,
  //     isShowProp: true
  //   })
  //   let hasUser = getApp().hasUser()
  //   if (!hasUser) {
  //     this.setData({
  //       showProp: true
  //     })
  //     return false
  //   } else {
  //     this.setData({
  //       showProp: false
  //     })
  //     return true
  //   }
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // var openid = wx.getStorageSync('openid');
    let user = wx.getStorageSync('wxUser')
    console.log("user",user)
    //判断是否登录
    // if (user == null) {
      this.valided()
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})